package main

import (
	"fmt"
	"math"
	"math/rand"
	"strings"
	"time"
)

func main() {
	//fmt.Println("hello")
	i, e := uint8ToInt(555)
	println(i, e)
	fmt.Printf("%d\n", i)
	var ch byte = 65
	fmt.Printf("%s\n", string(ch))
	//Randdoms()
	strings1()

}

func strings1() {
	s := "this is an example of string"
	fmt.Println("s字符串是否是th开头？", strings.HasPrefix(s, "th"))
	fmt.Println("s字符串是否是th结束？", strings.HasSuffix(s, "th"))
	a := "www.golang.com"
	fmt.Println("a是否包含golang？", strings.Contains(a, "golang"))

	//判断子字符串或字符在父字符串中出现的位置（索引）
	//如果 ch 是非 ASCII 编码的字符，建议使用以下函数来对字符进行定位：
	//strings.IndexRune(s string, r rune) int
	//	var str string = "Hi, I'm Marc, Hi."
	//	fmt.Printf("The position of \"Marc\" is: ")
	//	fmt.Printf("%d\n", strings.Index(str, "Marc"))
	//	fmt.Printf("The position of the first instance of \"Hi\" is: ")
	//	fmt.Printf("%d\n", strings.Index(str, "Hi"))
	//	fmt.Printf("The position of the last instance of \"Hi\" is: ")
	//	fmt.Printf("%d\n", strings.LastIndex(str, "Hi"))
	//	fmt.Printf("The position of \"Burger\" is: ")
	//	fmt.Printf("%d\n", strings.Index(str, "Burger"))
	//输出：
	//	The position of "Marc" is: 8
	//	The position of the first instance of "Hi" is: 0
	//	The position of the last instance of "Hi" is: 14
	//	The position of "Burger" is: -1

	//统计字符串出现的次数
	var str string = "Hello, how is it going, Hugo?"
	var manyG = "gggggggggg"

	fmt.Printf("Number of H's in %s is: ", str)
	fmt.Printf("%d\n", strings.Count(str, "H"))

	fmt.Printf("Number of double g's in %s is: ", manyG)
	fmt.Printf("%d\n", strings.Count(manyG, "gg"))
	//输出：
	//Number of H's in Hello, how is it going, Hugo? is: 2
	//Number of double g’s in gggggggggg is: 5
	var filed string = "a b c d"
	fileds := strings.Fields(filed)
	fmt.Println("fileds值：", fileds)

}

//go 随机数
func Randdoms() {
	//	for i := 0; i < 10; i++ {
	//		a := rand.Int()
	//		fmt.Printf("%d / \n", a)
	//	}
	//为什么生成的随机数总是一样的？？？因为rand是个伪随机生成器，而同一个共享源生成的随机数序列是恒定的。
	//	for i := 0; i < 1; i++ {
	//		r := rand.Intn(10)
	//		fmt.Printf("%d / \n", r)
	//	}
	fmt.Println("生成不一样的随机数：")
	timens := int64(time.Now().Nanosecond())
	rand.Seed(timens)
	for i := 0; i < 1; i++ {
		fmt.Printf("%2.2f / \n", 100*rand.Float32())
		fmt.Printf("%d / \n", rand.Intn(100))
	}
}
func uint8ToInt(n int) (uint8, error) {
	if 0 <= n && n < math.MaxInt8 {
		return uint8(n), nil
	}
	return 0, fmt.Errorf("%d is out uint8 range", n)
}

//拼接 slice 到字符串和分割字符串
func stringSplitJoin() {
	str := "The quick brown fox jumps over the lazy dog"
	sl := strings.Fields(str)
	fmt.Printf("Splitted in slice: %v\n", sl)
	for _, val := range sl {
		fmt.Printf("%s - ", val)
	}
	fmt.Println()
	str2 := "GO1|The ABC of Go|25"
	sl2 := strings.Split(str2, "|")
	fmt.Printf("Splitted in slice: %v\n", sl2)
	for _, val := range sl2 {
		fmt.Printf("%s - ", val)
	}
	fmt.Println()
	str3 := strings.Join(sl2, ";")
	fmt.Printf("sl2 joined by ;: %s\n", str3)

	//输出：
	//	Splitted in slice: [The quick brown fox jumps over the lazy dog]
	//	The - quick - brown - fox - jumps - over - the - lazy - dog -
	//	Splitted in slice: [GO1 The ABC of Go 25]
	//	GO1 - The ABC of Go - 25 -
	//	sl2 joined by ;: GO1;The ABC of Go;25
}

//时间包
func times() {
	t := time.Now()
	fmt.Println(t) // e.g. Wed Dec 21 09:52:14 +0100 RST 2011
	fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year())
	// 21.12.2011
	t = time.Now().UTC()
	fmt.Println(t)          // Wed Dec 21 08:52:14 +0000 UTC 2011
	fmt.Println(time.Now()) // Wed Dec 21 09:52:14 +0100 RST 2011
	// calculating times:
	week = 60 * 60 * 24 * 7 * 1e9 // must be in nanosec
	week_from_now := t.Add(week)
	fmt.Println(week_from_now) // Wed Dec 28 08:52:14 +0000 UTC 2011
	// formatting times:
	fmt.Println(t.Format(time.RFC822))         // 21 Dec 11 0852 UTC
	fmt.Println(t.Format(time.ANSIC))          // Wed Dec 21 08:56:34 2011
	fmt.Println(t.Format("02 Jan 2006 15:04")) // 21 Dec 2011 08:52
	s := t.Format("20060102")
	fmt.Println(t, "=>", s)
	// Wed Dec 21 08:52:14 +0000 UTC 2011 => 20111221
}
